﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using lib.Model;
using lib.DAL;
using Common.Unit;
namespace lib.BLL
{
    public partial class RoleManage_Bll
    {
        /// <summary>
        /// 保存角色以及菜单权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveRoleManageForm(RoleManage_Model model, List<RoleMenuManage_Model> roleManageModelList)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    if (model.FdRoleManageID.Value == 0)
                    {
                        model.FdRoleManageID = dal.Add(model, trans);
                    }
                    else
                    {
                        dal.Update(trans, model);
                        RoleMenuManage_Dal.Instance.Delete("fdRoleManageID=@fdRoleManageID", trans, new SqlParameter("@fdRoleManageID", model.FdRoleManageID));
                    }
                    List<int> selMenuIds = new List<int>();
                    //用户所选择权限
                    foreach (RoleMenuManage_Model roleMenuManageModel in roleManageModelList)
                    {
                        selMenuIds.Add(roleMenuManageModel.FdRoleMenuID.Value);
                        roleMenuManageModel.FdRoleManageID = model.FdRoleManageID;
                        RoleMenuManage_Dal.Instance.Add(roleMenuManageModel, trans);
                    }
                    //加载所选菜单的上级权限
                    if (selMenuIds != null && selMenuIds.Count > 0)
                    {
                        IList<MenuInfo_Model> parentMenuList = MenuInfo_Bll.Instance.GetModelList("fdMenuID in (" + selMenuIds.SeperateToString(",") + ")  group by fdMenuParentID", " fdMenuParentID", "");
                        if (parentMenuList != null && parentMenuList.Count > 0)
                        {
                            foreach (MenuInfo_Model menuInfoModel in parentMenuList)
                            {
                                RoleMenuManage_Model roleMenuManageModel = new RoleMenuManage_Model();
                                roleMenuManageModel.FdRoleMenuID = menuInfoModel.FdMenuParentID;
                                roleMenuManageModel.FdRoleMenuRoleCode = 1000;
                                roleMenuManageModel.FdRoleManageID = model.FdRoleManageID;
                                RoleMenuManage_Dal.Instance.Add(roleMenuManageModel, trans);
                            }
                        }
                    }
                    trans.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool delRoleManage(int roleId)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    dal.Delete(trans, roleId);
                    RoleMenuManage_Dal.Instance.Delete("fdRoleManageID=@fdRoleManageID", trans, new SqlParameter("@fdRoleManageID", roleId));
                    trans.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }
    }
}

